<?php

namespace App\Models;

use Hyn\Tenancy\Traits\UsesTenantConnection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Storage;


/**
 * App\Models\Attachment
 *
 * @property int $id
 * @property string $name
 * @property string|null $description
 * @property string|null $type 附件类型
 * @property string|null $host
 * @property int|null $size
 * @property mixed|null $meta 额外信息
 * @property int $upload_by 上传者
 * @property string|null $filename
 * @property int $attachable_id
 * @property string $attachable_type
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $attachable
 * @property-read mixed $download_url
 * @property-read mixed $preview_url
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Attachment onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereAttachableId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereAttachableType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereFilename($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereHost($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereMeta($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereSize($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Attachment whereUploadBy($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Attachment withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Attachment withoutTrashed()
 * @mixin \Eloquent
 */
class Attachment extends Model
{

    use SoftDeletes, UsesTenantConnection;
    public function attachable()
    {
        return $this->morphTo();
    }

    public function getDownloadUrlAttribute()
    {
        $disk = Storage::disk("oss");
        // 获取文件访问地址「公共读的 bucket 才生效」
        $url = $disk->getUrl($this->attributes['filename'], 86400);

        return $url;
    }

    public function getPreviewUrlAttribute()
    {
        $disk = Storage::disk("oss");
        // 设置文件访问有效期「$timeout 为多少秒过期」「私有 bucket 才可看见效果」
        // 24小时
         $url = $disk->signUrl($this->attributes['filename'], 86400, ['x-oss-process' => 'image/resize,w_200']);
         return $url;
    }
}
